package com.hundsun.documentripper.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hundsun.documentripper.domain.dto.BaseResponse;
import com.hundsun.documentripper.domain.pojo.ProductVersion;
import com.hundsun.documentripper.domain.query.BaseQuery;
import com.hundsun.documentripper.error.ErrorCatcher;
import com.hundsun.documentripper.service.ProductVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Note：
 *
 * @author ：mengjw
 * @description：产品版本控制层
 * @date ：Created in 2021/1/26
 */
@Api("产品版本")
@RestController
public class ProductVersionController {

    @Autowired
    private ProductVersionService productVersionService;

    @ApiOperation("新建产品版本")
    @PostMapping("/productVersion/")
    @ErrorCatcher(code = "3001")
    @Transactional
    public BaseResponse<Void> insertProductVersion(@RequestBody ProductVersion productVersion, HttpServletRequest request) {
        productVersion.setCreateUser((String) request.getSession().getAttribute("userUuid"));
        productVersionService.insertProductVersion(productVersion);
        return new BaseResponse<Void>();
    }

    @ApiOperation("更新产品版本信息")
    @PutMapping("/productVersion/")
    @ErrorCatcher(code = "3003")
    public BaseResponse<Void> updateProductVersion(@RequestBody ProductVersion productVersion, HttpServletRequest request) {
        productVersion.setModifyUser((String) request.getSession().getAttribute("userUuid"));
        productVersionService.updateProductVersion(productVersion);
        return new BaseResponse<Void>();
    }

    @ApiOperation("根据唯一标识获取产品版本对象")
    @GetMapping("/productVersion/{uuid}")
    @ErrorCatcher(code = "3004")
    public BaseResponse<ProductVersion> getProductVersionById(@PathVariable("uuid") String uuid) {
        return new BaseResponse<ProductVersion>(productVersionService.getProductVersionById(uuid));
    }

    @ApiOperation("删除产品版本")
    @DeleteMapping("/productVersion/{uuid}")
    @ErrorCatcher(code = "3005")
    public BaseResponse<Void> deleteProductVersion(@PathVariable("uuid") String uuid) {
        productVersionService.deleteProductVersion(uuid);
        return new BaseResponse<Void>();
    }

    @ApiOperation("分页获取产品的版本列表")
    @PostMapping("/product/{productUuid}/productVersion")
    @ErrorCatcher(code = "3006")
    public BaseResponse<IPage<ProductVersion>> getProductVersionListByPage(@RequestBody BaseQuery baseQuery, @PathVariable("productUuid") String productUuid) {
        return new BaseResponse<IPage<ProductVersion>>(productVersionService.getProductVersionListByPage(baseQuery, productUuid));
    }

    @ApiOperation("根据产品唯一标识获取最新产品版本")
    @GetMapping("/product/{productUuid}/lastProductVersion")
    @ErrorCatcher(code = "3007")
    public BaseResponse<ProductVersion> getLastProductVersion(@PathVariable("productUuid") String productUuid) {
        return new BaseResponse<ProductVersion>(productVersionService.getLastProductVersion(productUuid));
    }

    @ApiOperation("获取产品的全部版本")
    @GetMapping("/product/{productUuid}/productVersion")
    @ErrorCatcher(code = "3008")
    public BaseResponse<List<ProductVersion>> getAllProductVersion(@PathVariable("productUuid") String productUuid) {
        return new BaseResponse<List<ProductVersion>>(productVersionService.getAllProductVersion(productUuid, true));
    }

}